package com.zbl.jdbc;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class JdbcTest {

	private static Connection conn = null;
	private static PreparedStatement ps = null;
	private static ResultSet rs = null;
	private static final CallableStatement cs = null;

	// 查询
	public static int select(String te) {
		int i = 0;
		try {
			conn = JDBCUtils.getConnection();

			String sql = "SELECT * FROM zbl_member z where z.telephone =? ";

			ps = conn.prepareStatement(sql);
			ps.setString(1, te);
			rs = ps.executeQuery();

			while (rs.next()) {
				String id = rs.getString("id");
				String open_id = rs.getString("open_id");
				String telephone = rs.getString("telephone");
				String integral = rs.getString("integral");
				if(null!=integral) {
					i = Integer.valueOf(integral);
				}
				System.out.println("id=" + id + "  " + " open_id=" + open_id + "  telephone=" + telephone + " integral="
						+ integral);
			}
			return i;
		} catch (SQLException e) {
			System.out.println("操作失败o(╥﹏╥");
			e.printStackTrace();
		} finally {
			JDBCUtils.colseResource(conn, ps, rs);
		}
		return 0;
	}

	// 更新
	public static void update(int integral, String telephone) {

		try {

			conn = JDBCUtils.getConnection();
			String sql = "update zbl_member z set z.integral='" + integral + "' where z.telephone='" + telephone + "'";

			int i = 0;
			try {
				ps = (PreparedStatement) conn.prepareStatement(sql);
				i = ps.executeUpdate();
				System.out.println("resutl: " + i);
				ps.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (SQLException e) {
			System.out.println("操作失败o(╥﹏╥");
			e.printStackTrace();
		} finally {
			JDBCUtils.colseResource(conn, ps, rs);
		}
	}
	
	
	public static void upJF(int clubcard,String telephone) {
		
		int i = select(telephone);//获取当前用户已有积分
		int count=i+clubcard;
		update(count,telephone);//更新到表里
		System.out.println("update zbl_member === integral is  success");
		
		
	}
	
	

//	public void main() {
//
//		update(200, "13867556881");
//
//	}
}
